package com.kaiyidiannao.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.DisplayUtil;
import com.kaiyidiannao.common.util.MySystemParams;
import com.kaiyidiannao.common.waterfall_view.XListView;
import com.kaiyidiannao.main.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA by david.
 * Date: 2014/5/24 18:31
 * 带有图片（网络）显示的Adapter
 */
public abstract class ImageLoaderBaseAdapter extends BaseAdapter {

    protected DisplayImageOptions options;
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    private String TAG = "ImageLoaderBaseAdapter";

    protected ImageLoaderBaseAdapter() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_default)
                .showImageForEmptyUri(R.drawable.icon_default) //ic_empty
                .showImageOnFail(R.drawable.icon_default) //ic_error
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
    }

    public void clearDisplayImages() {
        AnimateFirstDisplayListener.displayedImages.clear();
    }

    protected ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

    protected static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    //图片加载成功的动画
                    //FadeInBitmapDisplayer.animate(imageView, 500);
                    //displayedImages.add(imageUri);
                }
            }
        }
    }

    /**
     * 调节瀑布流图片尺寸
     *
     * @param imageView
     * @param thumbStr
     * @param parentListview
     */
    protected void ajastImageViewSize(ImageView imageView, String thumbStr, ViewGroup parentListview, Context context) {
        // 设置图片大小
        if (!TextUtils.isEmpty(thumbStr)) {
            int column = ((XListView) parentListview).getColumnCount();
            Activity activity = (BaseActionBarActivity) context;
            float density = activity.getResources().getDisplayMetrics().density;  // 屏幕密度（0.75 / 1.0 / 1.5）
            int listviewWidth = MySystemParams.getInstance(activity).screenWidth - DisplayUtil.dip2px(3 * 2, density);

            Log.d("density：", "density:" + density);

//            String filePath = imageLoader.getDiskCache().get(thumbStr).toString();
//            BitmapFactory.Options options = new BitmapFactory.Options();
//            options.inJustDecodeBounds = true;
//            BitmapFactory.decodeFile(filePath, options);
//            int imageHeight = options.outHeight;
//            int imageWidth = options.outWidth;

            float item_width = listviewWidth / column;
            float layoutHeight = context.getResources().getDimension(R.dimen.waterfull_image_height); //200dp
            try {
//                imageView.setMaxWidth((int)item_width);
//                layoutHeight = (imageHeight * item_width) / imageWidth;// 调整高度
//                layoutHeight = DisplayUtil.dip2px(layoutHeight, context);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) item_width, (int) layoutHeight);
            imageView.setLayoutParams(layoutParams);
        }
    }
}
